package com.mic.controller;

import com.mic.pojo.Article;
import com.mic.pojo.Comment;
import com.mic.pojo.User;
import com.mic.pojo.UserFavorite;
import com.mic.service.AdminServiceImpl;
import com.mic.service.UserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    @Qualifier("AdminServiceImpl")
    AdminServiceImpl adminService;

    @Autowired
    @Qualifier("UserServiceImpl")
    UserServiceImpl userService;


    @RequestMapping("/logout")
    public void logout(HttpServletResponse response, HttpServletRequest request) throws IOException {
        response.sendRedirect(request.getContextPath()+"/toLogin");
    }

    @RequestMapping("/toUserPersonalPage")
    public String toUserPersonalPage(HttpSession session, Model model){
        Integer userId = (Integer) session.getAttribute("userId");
        model.addAttribute("userId",userId);//model将userId的属性值传给了UserPersonalPage
        User user = adminService.queryUserByUserId(userId);
        List<Article> articles = userService.queryTopFourArticles();
        List<Article> userFavoriteArticles = userService.queryFiveUserFavoriteArticlesByUserId(userId);
        List<Article> userArticles = userService.queryAllUserArticlesByUserId(userId);
        model.addAttribute("userArticles",userArticles);
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        model.addAttribute("articles",articles);
        model.addAttribute("user",user);



        return "UserPersonalPage";



    }
    @RequestMapping("/toBlogSquare")
    public String toBlogSquare(HttpSession session, Model model){
        Integer userId = (Integer) session.getAttribute("userId");
        model.addAttribute("userId",session.getAttribute("userId"));//model将userId的属性值传给了BlogSquare
        List<Article> userFavoriteArticles = userService.queryFiveUserFavoriteArticlesByUserId(userId);//五个收藏
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        List<Article> articles = userService.queryTopFourArticles();//四个热门top4
        model.addAttribute("articles",articles);
        List<Article> allArticles = userService.queryAllArticlesByTime(userId);//按时间展示所有的文章
        model.addAttribute("allArticles",allArticles);

        Article hotUserArticle = userService.hotUser();//热门博主
        model.addAttribute("hotUserArticle",hotUserArticle);

        List<Article> hotUserArticles = userService.decsSixArticlesByLikes(hotUserArticle.getBlogId());
        model.addAttribute("hotUserArticles",hotUserArticles);
        return "BlogSquare";
    }

    @RequestMapping("/toUserFavorite")
    public String toUserFavorite(HttpSession session, Model model){
        Integer userId = (Integer) session.getAttribute("userId");
        List<Article> articles = userService.queryTopFourArticles();
        List<Article> userFavoriteArticles = userService.queryAllUserFavoriteArticlesByUserId(userId);
        model.addAttribute("articles",articles);
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        model.addAttribute("userId",session.getAttribute("userId"));//model将userId的属性值传给了UserFavorite
        return "UserFavorite";
    }

    @RequestMapping("/toSubscription")
    public String toSubscription(HttpSession session, Model model){
        Integer userId = (Integer) session.getAttribute("userId");
        model.addAttribute("userId",session.getAttribute("userId"));//model将userId的属性值传给了Subscription
        List<User> userSubscriptions = userService.queryAllUserSubscriptionById(userId);
        model.addAttribute("userSubscriptions",userSubscriptions);
        return "Subscription";
    }
    @RequestMapping("/toWritingPage")
    public String toWritingPage(HttpSession session, Model model){
        Integer userId = (Integer) session.getAttribute("userId");
        List<Article> userFavoriteArticles = userService.queryFiveUserFavoriteArticlesByUserId(userId);//五个收藏
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        List<Article> articles = userService.queryTopFourArticles();//四个热门top4
        model.addAttribute("articles",articles);
        return "WritingPage";
    }

    @RequestMapping("/toUserSearchResult")
    public String toUserSearchResult(HttpSession session, Model model, @Param("key") String key){
        Integer userId = (Integer) session.getAttribute("userId");
        List<Article> userFavoriteArticles = userService.queryFiveUserFavoriteArticlesByUserId(userId);//五个收藏
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        List<Article> articles = userService.queryTopFourArticles();//四个热门top4
        model.addAttribute("articles",articles);

        List<User> resultUsers = userService.searchAllRelativeUsersByKeyEqualsUsername(key);        //搜索框功能
        model.addAttribute("resultUsers",resultUsers);        //搜索框功能
        List<Article> resultArticles = userService.searchAllRelativeArticlesByKeyEqualsTitleOrInShort(key);     //搜索框功能
        model.addAttribute("resultArticles",resultArticles);        //搜索框功能

        return "UserSearchResult";
    }

    @RequestMapping("/toArticlePage")
    public String toMainUser(HttpSession session, Model model,Integer certainArticleId){
        Integer userId = (Integer) session.getAttribute("userId");
        List<Article> userFavoriteArticles = userService.queryFiveUserFavoriteArticlesByUserId(userId);//五个收藏
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        List<Article> articles = userService.queryTopFourArticles();//四个热门top4
        model.addAttribute("articles",articles);

        List<Comment> comments = userService.queryAllCommentsByArticleId(certainArticleId);
        model.addAttribute("comments",comments);

        Article certainArticle = userService.searchArticleByCertainArticleId(certainArticleId);
        model.addAttribute("certainArticle",certainArticle);

        if(certainArticle.getBlogId().equals(userId))
            return "PersonalArticlePage";
        else return "ArticlePage";
    }
    @RequestMapping("/toOthersPersonalPage")
    public String toOthersPersonalPage(HttpSession session, Model model,Integer certainUserId){
        Integer userId = (Integer) session.getAttribute("userId");
        List<Article> userFavoriteArticles = userService.queryFiveUserFavoriteArticlesByUserId(userId);//五个收藏
        model.addAttribute("userFavoriteArticles",userFavoriteArticles);
        List<Article> articles = userService.queryTopFourArticles();//四个热门top4
        model.addAttribute("articles",articles);

        User certainUser = userService.searchUserByCertainUserId(certainUserId);
        model.addAttribute("certainUser",certainUser);//把这个人放进去

        List<Article> certainUserArticles = userService.queryAllUserArticlesByUserId(certainUserId);
        model.addAttribute("certainUserArticles",certainUserArticles);//把这个人的所有文章放进去
        return "OthersPersonalPage";
    }




}
